﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ScytaleDecrypt.Model
{
    static class DictionaryComparer {

        private static Dictionary Dictionary { get; set; }

        private static string originalMessage;

        private static int WORD_MIN_LENGTH = 4;

        private static int WORD_MAX_LENGTH = 12; 

        public static int foundWords;

        public static List<String> allWords;  

        public static List<string> foundWordsList;  

        public static void CheckMessageForWords(string message) {
            originalMessage = message;
            foundWords = 0;
            foundWordsList = new List<string>();
            allWords = new List<string>();
            WORD_MIN_LENGTH = 4;
            WORD_MAX_LENGTH = 15;
            Dictionary = Dictionary.GetInstance;
            for (int startPosition = 0; startPosition < message.Length; startPosition++) {
                CreateWordsAndCheckDictionary(startPosition);
            }
        }

        private static void CreateWordsAndCheckDictionary(int startPosition) {
            for (int currentWordLength = WORD_MIN_LENGTH; (currentWordLength < WORD_MAX_LENGTH) && (startPosition + currentWordLength < originalMessage.Length); currentWordLength++)
            {
                String createdWord = originalMessage.Substring(startPosition, currentWordLength);
                allWords.Add(createdWord);
                allWords.Sort();
                if (IsInDictionary(createdWord))
                {
                    foundWordsList.Add(createdWord);
                    foundWords++;
                }
            }
            return;
        }

        private static bool IsInDictionary( string word ) {
            if (!Dictionary.Contains(word.ToLower())) {
                return false;
            }
            return true; 
        }
    }
}
